package com.onluy.frameworkcloud.tools.utils;


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

public class ReflectionUtils {
    public static void setFieldValue(Object target, String propertyName, Object propertyValue) throws Exception {
        if (target != null && propertyName != null && !"".equals(propertyName)) {
            Field field = getDeclaredField(target, propertyName);
            field.setAccessible(true);
            field.set(target, propertyValue);
        }
    }

    public static Object getFieldValue(Object target, String property) throws Exception {
        if (target != null && property != null && !"".equals(property)) {
            if (target instanceof Map) {
                return ((Map) target).get(property) == null ? "0" : ((Map) target).get(property).toString();
            } else {
                Class clazz = target.getClass();
                String getterName = getGetterName(property);
                Method method = clazz.getDeclaredMethod(getterName, new Class[0]);
                return method.invoke(target, new Object[0]);
            }
        } else {
            return null;
        }
    }

    public static Object invoke(Object target, String methodName, Object[] params, Class<?>[] paramsClass)
            throws Exception {
        if (target != null && methodName != null && !"".equals(methodName)) {
            Method method = getDeclaredMethod(target, methodName, paramsClass);
            method.setAccessible(true);
            Object object = method.invoke(target, params);
            return object;
        } else {
            return null;
        }
    }

    public static Field getDeclaredField(Object object, String fieldName) {
        Class clazz = object.getClass();
        Field field = null;

        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (Exception arg6) {
            while (clazz != Object.class) {
                try {
                    return clazz.getDeclaredField(fieldName);
                } catch (Exception arg5) {
                    clazz = clazz.getSuperclass();
                }
            }
        }

        return field;
    }

    public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] paramsClass) {
        Class clazz = object.getClass();
        Method method = null;

        try {
            method = clazz.getDeclaredMethod(methodName, paramsClass);
        } catch (Exception arg7) {
            while (clazz != Object.class) {
                try {
                    return clazz.getDeclaredMethod(methodName, paramsClass);
                } catch (Exception arg6) {
                    clazz = clazz.getSuperclass();
                }
            }
        }

        return method;
    }

    public static String getGetterName(String property) {
        if (property != null && property.length() != 0) {
            String str = "get" + property.substring(0, 1).toUpperCase();
            if (property.length() > 1) {
                str = str + property.substring(1);
            }

            return str;
        } else {
            return "";
        }
    }
}